package com.hs.telepass.controller.task;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import com.hs.telepass.MainApp;
import com.hs.telepass.controller.TaskController;
import com.hs.telepass.controller.executor.JsonTaskExecutor;
import com.hs.telepass.controller.executor.TaskExecutor;


import java.io.File;
import java.io.Serializable;

public abstract class Task implements Serializable {


    public static final String BASE_API_URL = "http://sg.open.com.mo";
    public static final String BASE_ORG_API_URL = "http://test.singou.mo";

    public static final int TASK_STATUS_READY = 0;
    public static final int TASK_STATUS_RUNNING = 1;
    public static final int TASK_STATUS_CANCELED = 2;
    public static final int TASK_STATUS_END = 3;

    public static final int TASK_REQUST_TYPE_POST = 0;
    public static final int TASK_REQUST_TYPE_PUT = 1;
    public static final int TASK_REQUST_TYPE_GET = 2;
    public static final int TASK_REQUST_TYPE_DELETE = 3;


    protected int taskState;
    protected TaskExecutor executor;

    protected LocalBroadcastManager broadcastManager;

    public Task() {
        taskState = TASK_STATUS_READY;
        broadcastManager = LocalBroadcastManager.getInstance(MainApp.getInstance());
    }

    public void onBegin() {
        taskState = TASK_STATUS_RUNNING;
        Intent intent = new Intent(getActionName());
        intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_BEGIN);
        broadcastManager.sendBroadcast(intent);
    }

    ;

    public void execute(Context context, Task task) {
        executor = new JsonTaskExecutor(task);
        executor.execute(context);
    }

    ;

    public void onProgress(long done, long total) {

    }

    ;

    public void onExecuted(String response, int httpCode) {
    }

    ;

    /**
     * 当请求发生错误时,服务器返回500
     */
    public void onExecutedFail(String response) {
        Intent intent = new Intent(getActionName());
        intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_FAILED);
        intent.putExtra(TaskController.EXTRA_RESPONSE, response);
        broadcastManager.sendBroadcast(intent);
    }

    public void onError(int errorCode) {
        Intent intent = new Intent(getActionName());
        intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_ERROR);
        intent.putExtra(TaskController.EXTRA_RESPONSE, errorCode);
        broadcastManager.sendBroadcast(intent);
    }

    ;

    public void onEnd() {
        taskState = TASK_STATUS_END;
        Intent intent = new Intent(getActionName());
        intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_END);
        broadcastManager.sendBroadcast(intent);
    }

    ;

    public void cancel() {
        taskState = TASK_STATUS_CANCELED;
        if (executor != null) {
            executor.cancel();
        }
    }

    ;

    /**
     * 获得当前任务的状态
     */
    public int getTaskState() {
        return taskState;
    }

    public File getFile(){
        return null;
    }


    /**
     * 获取请求的类型
     */
    public abstract int getRequstType();

    /**
     * 获得请求xml的str格式
     */
    public abstract String getJsonRequstStr();

    /**
     * 获得该任务提交成功后服务器返回的成功码
     */
    public abstract int getResponseSuccessCode();

    /**
     * 获得提交该任务的url路径
     */
    public abstract String getTaskUrl();

    /**
     * 获得对应的用于广播的action name
     */
    public abstract String getActionName();

}
